<?php
namespace Hilaoyu\ComputeCloudClient\Api;


use Hilaoyu\ComputeCloudClient\HuaWeiCloudApi;
use Hilaoyu\Utils\Traits\ErrorsTrait;
use Hilaoyu\Utils\UtilArr;
use HuaweiCloud\SDK\Core\Client;


class HuaWeiApiBase
{
    use ErrorsTrait;
    protected $client;
    protected $api;
    protected $regionId;


    protected $_clientVpcV3;
    protected $_clientVpcV2;
    protected $_clientEip;
    protected $_clientEcs;
    protected $_clientIms;
    protected $_clientEvs;

    protected $regins = [
        ["id" => "af-south-1", "name" => '非洲-约翰内斯堡'],
        ["id" => "cn-north-4", "name" => '华北-北京四'],
        ["id" => "cn-north-1", "name" => '华北-北京一'],
        ["id" => "cn-east-2", "name" => '华东-上海二'],
        ["id" => "cn-east-3", "name" => '华东-上海一'],
        ["id" => "cn-south-1", "name" => '华南-广州'],
        ["id" => "cn-south-2", "name" => '华南-深圳'],
        ["id" => "cn-southwest-2", "name" => '西南-贵阳一'],
        ["id" => "ap-southeast-2", "name" => '亚太-曼谷'],
        ["id" => "ap-southeast-3", "name" => '亚太-新加坡'],
        ["id" => "ap-southeast-1", "name" => '中国-香港'],
        ["id" => "cn-north-9", "name" => '华北-乌兰察布一'],
    ];

    protected $regionZones = [
        'cn-north-9' => [
            ["id" => "cn-north-9a", "name" => '可用区1'],
            ["id" => "cn-north-9b", "name" => '可用区2'],
        ],
        'af-south-1' => [
            ["id" => "af-south-1a", "name" => '可用区1'],
            ["id" => "af-south-1b", "name" => '可用区2'],
        ],
        'cn-north-4' => [
            ["id" => "cn-north-4a", "name" => '可用区1'],
            ["id" => "cn-north-4b", "name" => '可用区2'],
            ["id" => "cn-north-4c", "name" => '可用区3'],
            ["id" => "cn-north-4g", "name" => '可用区7'],
        ],
        'cn-north-1' => [
            ["id" => "cn-north-1a", "name" => '可用区1'],
            ["id" => "cn-north-1b", "name" => '可用区2'],
            ["id" => "cn-north-1c", "name" => '可用区3'],
        ],
        'cn-east-2' => [
            ["id" => "cn-east-2a", "name" => '可用区1'],
            ["id" => "cn-east-2b", "name" => '可用区2'],
            ["id" => "cn-east-2c", "name" => '可用区3'],
            ["id" => "cn-east-2d", "name" => '可用区4'],
        ],
        'cn-east-3' => [
            ["id" => "cn-east-3a", "name" => '可用区1'],
            ["id" => "cn-east-3b", "name" => '可用区2'],
            ["id" => "cn-east-3c", "name" => '可用区3'],
        ],
        'cn-south-1' => [
            ["id" => "cn-south-1a", "name" => '可用区1'],
            ["id" => "cn-south-1b", "name" => '可用区2'],
            ["id" => "cn-south-1c", "name" => '可用区3'],
            ["id" => "cn-south-1e", "name" => '可用区5'],
            ["id" => "cn-south-1f", "name" => '可用区6'],
        ],
        'cn-southwest-2' => [
            ["id" => "cn-southwest-2a", "name" => '可用区1'],
            ["id" => "cn-southwest-2b", "name" => '可用区2'],
            ["id" => "cn-southwest-2c", "name" => '可用区3'],
            ["id" => "cn-southwest-2d", "name" => '可用区4'],
            ["id" => "cn-southwest-2e", "name" => '可用区5'],
            ["id" => "cn-southwest-2f", "name" => '可用区6'],
        ],
        'ap-southeast-2' => [
            ["id" => "ap-southeast-2a", "name" => '可用区1'],
            ["id" => "ap-southeast-2b", "name" => '可用区2'],
        ],
        'ap-southeast-3' => [
            ["id" => "ap-southeast-3a", "name" => '可用区1'],
            ["id" => "ap-southeast-3b", "name" => '可用区2'],
            ["id" => "ap-southeast-3c", "name" => '可用区3'],
        ],
        'ap-southeast-1' => [
            ["id" => "ap-southeast-1a", "name" => '可用区1'],
            ["id" => "ap-southeast-1b", "name" => '可用区2'],
        ],
    ];
    protected $privateNameServers = [
        'af-south-1' => [
            "100.125.1.250",
            "100.125.1.14",
        ],
        'cn-north-4' => [
            "100.125.1.250",
            "100.125.129.250",
        ],
        'cn-north-1' => [
            "100.125.1.250",
            "100.125.21.250",
        ],
        'cn-east-2' => [
            "100.125.17.29",
            "100.125.135.29",
        ],
        'cn-east-3' => [
            "100.125.1.250",
            "100.125.64.250",
        ],
        'cn-south-1' => [
            "100.125.1.250",
            "100.125.136.29",
        ],
        'cn-southwest-2' => [
            "100.125.1.250",
            "100.125.129.250",
        ],
        'ap-southeast-2' => [
            "100.125.1.250",
            "100.125.1.251",
        ],
        'ap-southeast-3' => [
            "100.125.1.250",
            "100.125.128.250",
        ],
        'ap-southeast-1' => [
            "100.125.1.250",
            "100.125.3.250",
        ],
    ];

    public function __construct(Client $client ,HuaWeiCloudApi $api,$regionId=''){
        $this->client = $client;
        $this->api = $api;
        $this->regionId = trim($regionId);
    }

    protected function getRegionId($regionId=''){
        $regionId = trim($regionId);
        if(!$regionId){
            $regionId = $this->regionId;
        }
        return $regionId;
    }


    public function getClient(){
        return $this->client;
    }
    /**
     * @return \HuaweiCloud\SDK\Vpc\V2\VpcClient
     */
    public function clientVpcV2(){
        if (!$this->_clientVpcV2) {
            $this->_clientVpcV2 = $this->api->vpcV2($this->getRegionId())->getClient();
        }
        return $this->_clientVpcV2;
    }


    /**
     * @return \HuaweiCloud\SDK\Vpc\V3\VpcClient
     */
    public function clientVpcV3()
    {
        if (!$this->_clientVpcV3) {
            $this->_clientVpcV3 = $this->api->vpcV3($this->getRegionId())->getClient();
        }
        return $this->_clientVpcV3;
    }

    /**
     * @return \HuaweiCloud\SDK\Eip\V2\EipClient
     */
    public function clientEip()
    {
        if (!$this->_clientEip) {
            $this->_clientEip = $this->api->eip($this->getRegionId())->getClient();
        }
        return $this->_clientEip;
    }

    /**
     * @return \HuaweiCloud\SDK\Ecs\V2\EcsClient
     */
    public function clientEcs()
    {
        if (!$this->_clientEcs) {
            $this->_clientEcs = $this->api->ecs($this->getRegionId())->getClient();
        }
        return $this->_clientEcs;
    }
    /**
     * @return \HuaweiCloud\SDK\Ims\V2\ImsClient
     */
    public function clientIms()
    {
        if (!$this->_clientIms) {
            $this->_clientIms = $this->api->ims($this->getRegionId())->getClient();
        }
        return $this->_clientIms;
    }
    /**
     * @return \HuaweiCloud\SDK\Evs\V2\EvsClient
     */
    public function clientEvs()
    {
        if (!$this->_clientEvs) {
            $this->_clientEvs = $this->api->evs($this->getRegionId())->getClient();
        }
        return $this->_clientEvs;
    }

    public function regionsDescribe(): array
    {
        $models = $this->regins;

        return $models;
    }

    public function zonesDescribe($regionId = ''): array
    {
        $regionId = $this->getRegionId($regionId);
        if (!$regionId) {
            return [];
        }


        return UtilArr::get($this->regionZones, $regionId, []);
    }

    public function privateNameServersDescribe($regionId = ''): array
    {
        $regionId = $this->getRegionId($regionId);
        if (!$regionId) {
            return [];
        }


        $nemaServers =  UtilArr::get($this->privateNameServers, $regionId);
        if(empty($nemaServers)){
            $nemaServers = [
                "114.114.114.114",
                "8.8.8.8",
            ];
        }

        return $nemaServers;
    }



    public function formatStatus(string $type,string $status){

        $arr = [
            'sg' => [
            ],
            'vpc' => [
                'CREATING' => 'Pending',
                'OK' => 'Available',
            ],
            'vsw' => [
                'ACTIVE' => 'Available',
                'UNKNOWN' => 'Available',
                'ERROR' => 'Pending',
            ],
            'ecs' => [
                'BUILD' => 'Pending',
                'REBOOT' => 'Pending',
                'HARD_REBOOT' => 'Starting',
                'REBUILD' => 'Pending',
                'MIGRATING' => 'Pending',
                'RESIZE' => 'Pending',
                'ACTIVE' => 'Running',
                'SHUTOFF' => 'Stopped',
                'REVERT_RESIZE' => 'Pending',
                'VERIFY_RESIZE' => 'Pending',
                'ERROR' => 'Pending',
                'DELETED' => 'Deleted',
            ],
            'eip' => [
                'FREEZED' => 'Pending',
                'BIND_ERROR' => 'Pending',
                'BINDING' => 'Associating',
                'PENDING_DELETE' => 'Pending',
                'PENDING_CREATE' => 'Pending',
                'NOTIFYING' => 'Pending',
                'NOTIFY_DELETE' => 'Pending',
                'PENDING_UPDATE' => 'Pending',
                'DOWN' => 'Available',
                'ACTIVE' => 'InUse',
                'ELB' => 'InUse',
                'VPN' => 'InUse',
                'ERROR' => 'Pending',
            ],
            'vtb' => [
                '0' => 'Pending',
                '1' => 'Available',
            ],
            'eni' => [
                'DOWN' => 'Available',
                'BUILD' => 'Pending',
                'ACTIVE' => 'InUse',
            ],
            'cmd' => [
            ],
            'disk' => [
                'creating' => 'Creating',
                'available' => 'Available',
                'in-use' => 'In_use',
                'error' => 'Pending',
                'attaching' => 'Attaching',
                'detaching' => 'Detaching',
                'restoring-backup' => 'Pending',
                'backing-up' => 'Pending',
                'error_restoring' => 'Pending',
                'uploading' => 'Pending',
                'downloading' => 'Pending',
                'extending' => 'Pending',
                'error_extending' => 'Pending',
                'deleting' => 'Pending',
                'error_deleting' => 'Pending',
                'rollbacking' => 'ReIniting',
                'error_rollbacking' => 'Pending',
                'awaiting-transfer' => 'Pending',
            ],
            'cen' => [
            ],
        ];
        if(!$type || !$status){
            return "";
        }

        return UtilArr::get($arr,$type.'.'.$status,'');
    }



    public function formatPayChangeType( $type){

        $arr = [
            '0' => 'PostPaid',
            '1' => 'PrePaid',
            '2' => 'Biding',
        ];

        return UtilArr::get($arr,$type,'');
    }


    protected function getPublicIpType($regionId='')
    {
        $regionId = $this->getRegionId($regionId);
        $ipType = '5_bgp';
        switch ($regionId){
            case "cn-southwest-2":
                $ipType = '5_sbgp';
                break;
            default:
                break;
        }

        return $ipType;
    }

}
